#define  _CRT_SECURE_NO_WARNINGS
class Solution
{
public:
    ListNode* swapPairs(ListNode* head)
    {
        if (head == nullptr || head->next == nullptr) return head;
        ListNode* newHead = new ListNode(0);
        newHead->next = head;
        ListNode* prev = newHead, * cur = prev->next, * next = cur->next, * nnext
            = next->next;
        while (cur && next)
        {

            prev->next = next;
            next->next = cur;
            cur->next = nnext;
           
            prev = cur; 
            cur = nnext;
            if (cur) next = cur->next;
            if (next) nnext = next->next;
        }
        cur = newHead->next;
        delete newHead;
        return cur;
    }
};